﻿
Namespace internalFunctions
    'parameters, min, float/int, the lower bound, default value is 0
    '            max, float/int, the upper bound, default value is 1
    '            int, bool, whether output an integer, default value is false
    'return,     float/int, an random value between [min, max),
    '            whether the output is float or int is based on int parameter
    Public Class rnd
        Inherits internalFunction

        Public Class parameters
            Public Const min As String = "min"
            Public Const max As String = "max"
            Public Const int As String = "int"
        End Class

        Private Shared m As iParametersMapping = Nothing

        Shared Sub New()
            initialMapping(m, parameters.min, parameters.max, parameters.int)
        End Sub

        Public Sub New()
            MyBase.New("rnd", m)
        End Sub

        Protected Overrides Function invoke(ByVal ctx As context, ByRef output As bare_variable) As Boolean
            Dim min As Double = 0
            Dim max As Double = 0
            Dim int As Boolean = False

            If Not ctx.findVariableFloat(parameters.min, min) Then
                min = 0
            End If
            If Not ctx.findVariableFloat(parameters.max, max) Then
                max = 1
            End If
            If Not ctx.findVariableBool(parameters.int, int) Then
                int = False
            End If

            Dim rtn As Double
            rtn = Global.rnd(min, max, int)
            If int Then
                output = New bare_variable(typeDefination.int, Convert.ToInt64(rtn))
            Else
                output = New bare_variable(typeDefination.float, rtn)
            End If
            Return True
        End Function
    End Class

    'parameters, count, int, the count of guid need to create,
    '                        default value is 1
    '            separator, string, the separator between each guid,
    '                               default value is default value of separator of Global.createGUID function
    'return, string, a guid string
    Public Class createGUID
        Inherits internalFunction

        Public Class parameters
            Public Const count As String = "count"
            Public Const separator As String = "separator"
        End Class

        Public Sub New()
            MyBase.New("createGUID")
        End Sub

        Protected Overrides Function invoke(ByVal ctx As context, ByRef output As bare_variable) As Boolean
            Dim count As Int64 = 0
            If Not ctx.findVariableInt(parameters.count, count) Then
                count = 1
            End If

            Dim separator As String = Nothing
            If Not ctx.findVariableStr(parameters.separator, separator) Then
                separator = Nothing
            End If

            Dim rtn As String = Nothing
            If count = 1 Then
                rtn = Global.createGUID()
            ElseIf separator Is Nothing Then
                rtn = Global.createGUID(count)
            Else
                rtn = Global.createGUID(count, separator)
            End If
            output = New bare_variable(typeDefination.string, rtn)

            Return True
        End Function
    End Class
End Namespace